package io.lkt.modules.sys.controller;


import io.lkt.common.redis.RedisCacheUtil;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;

import io.lkt.modules.sys.entity.SysGzh2Entity;
import io.lkt.modules.sys.service.SysGzhService2;
import io.lkt.modules.sys.service.SysUserRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 公众号信息
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-12-21 14:48:00
 */
@RestController
@RequestMapping("/sys/sysgzh2")
public class SysGzhController2 extends AbstractController {
    @Autowired
    private SysGzhService2 sysGzhService2;


    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    RedisCacheUtil redisCacheUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:sysgzh:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysGzhService2.queryPage(params);
        List<SysGzh2Entity> list = (List<SysGzh2Entity>) page.getList();
        for (SysGzh2Entity sysGzh2Entity : list) {
            sysGzh2Entity.setUserId(sysGzh2Entity.getId());
        }
        page.setList(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:sysgzh:info")
    public R info(@PathVariable("id") String gzhCode) {
        SysGzh2Entity sysGzh = sysGzhService2.getById(gzhCode);
        return R.ok().put("sysgzh2", sysGzh);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:sysgzh:save")
    public R save(@RequestBody SysGzh2Entity sysGzh2) {
        sysGzh2.setCreateTime(new Date());
        sysGzhService2.save(sysGzh2);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:sysgzh:update")
    public R update(@RequestBody SysGzh2Entity sysGzh) {
        ValidatorUtils.validateEntity(sysGzh);
        sysGzhService2.updateById(sysGzh);

        redisCacheUtil.removeSingleCacheMaster("PlatformSysInfo:" + sysGzh.getGzhCode());
        redisCacheUtil.removeSingleCacheMaster("PlatformSysInfo:" + sysGzh.getAppId());
        redisCacheUtil.removeSingleCacheMaster("GzhCodeRealm:" + sysGzh.getGzhCode());
        redisCacheUtil.removeSingleCache("PlatformSysInfo:" + sysGzh.getGzhCode());
        redisCacheUtil.removeSingleCache("PlatformSysInfo:" + sysGzh.getAppId());
        redisCacheUtil.removeSingleCache("GzhCodeRealm:" + sysGzh.getGzhCode());

        return R.ok();
    }

    @RequestMapping("/updateFansCount")
    public R updateFansCount(@RequestBody Map<String, Object> params) {
        if (params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.error("访问失败！");
        }
        sysGzhService2.updateFansCount(params);
        return R.ok();
    }
}
